import { createRouter, createWebHistory } from 'vue-router'
import HotelDetailView from '../views/HotelDetailView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/HomeView.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/RegisterView.vue'),
    },
    {
      path: '/forgot',
      name: 'forgot',
      component: () => import('@/views/ForgotPasswordView.vue'),
    },
    {
      path: '/profile',
      name: 'profile',
      component: () => import('@/views/ProfileView.vue'),
    },
    {
      path: '/review/:hotelId/:bookingId',
      name: 'add-review',
      component: () => import('@/views/AddReview.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/admin/login',
      name: 'admin-login',
      component: () => import('@/views/admin/AdminLogin.vue'),
    },
    {
      path: '/admin',
      component: () => import('@/views/admin/AdminLayout.vue'),
      children: [
        {
          path: '',
          redirect: '/admin/hotel',
        },
        {
          path: 'hotel',
          name: 'hotel',
          component: () => import('@/views/admin/HotelManage.vue'),
        },
        {
          path: 'location',
          name: 'location',
          component: () => import('@/views/admin/LocationManage.vue'),
        },
        {
          path: 'amenities',
          name: 'amenities',
          component: () => import('@/views/admin/AmenitiesManage.vue'),
        },
        {
          path: 'policy',
          name: 'policy',
          component: () => import('@/views/admin/PolicyManage.vue'),
        },
        {
          path: 'room',
          name: 'room',
          component: () => import('@/views/admin/RoomManage.vue'),
        },
        {
          path: 'order',
          name: 'order',
          component: () => import('@/views/admin/OrderManage.vue'),
        },
        {
          path: 'order-statistics',
          name: 'order-statistics',
          component: () => import('@/views/admin/OrderStatistics.vue'),
        },
        {
          path: 'reviews',
          name: 'reviews',
          component: () => import('@/views/admin/ReviewsManage.vue'),
        },
        {
          path: 'customer',
          name: 'customer',
          component: () => import('@/views/admin/CustomerManage.vue'),
        },
      ],
    },
    {
      path: '/hotel/:id',
      name: 'hotel-detail',
      component: HotelDetailView
    }
  ],
})

// 路由守卫：后台页面需要登录
router.beforeEach((to, from, next) => {
  // 管理员页面权限检查
  if (to.path.startsWith('/admin') && to.path !== '/admin/login') {
    const isAdminLogin = localStorage.getItem('admin_token');
    if (!isAdminLogin) {
      return next('/admin/login');
    }
  }
  
  // 普通用户页面权限检查
  if (to.meta.requiresAuth) {
    const isUserLogin = localStorage.getItem('token');
    if (!isUserLogin) {
      return next(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
    }
  }
  
  next();
});

export default router
